package cn.hx.mall.goods.controller;

import cn.hx.mall.goods.entity.*;
import cn.hx.mall.goods.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@MultipartConfig
@Controller
@RequestMapping("goods")
public class ProductAdditionController {
    @Resource
    private ProductService productService;
    @Resource
    private BrandService brandService;
    @Resource
    private ProductCategoryService productCategoryService;
    @Resource
    private ProductAttributeCategoryService pacService;
    @Resource
    private SkuStockService skuStockService;


    private void load(Model model){
        List<Brand> brands = brandService.queryAll(null);
        List<ProductCategory> productCategories = productCategoryService.queryAll(null);
        model.addAttribute("category",productCategories);
        model.addAttribute("brands",brands);
    }

    @GetMapping("/productAdd")
    public String ProductAdd(Long id, Model model){
        load(model);
        if (id!=null){
            model.addAttribute("pro",productService.queryById(id));
            return "goods/productAdd";
        }
        return "goods/productAdd";
    }

    @PostMapping("/productAdd")
    public ModelAndView productAdd(Product product,Model model){
        load(model);
        ModelAndView modelAndView = new ModelAndView("goods/productAdd");
        modelAndView.addObject("product",product);
        return modelAndView;
    }

    @PostMapping("/productSell")
    public ModelAndView productSell(Product product,HttpSession session){
        session.setAttribute("product",product);
        ModelAndView modelAndView = new ModelAndView("goods/productSell");
        modelAndView.addObject("product",product);
        return modelAndView;
    }

    @PostMapping("/productAttribute")
    public String productAttribute( MultipartFile file, Product product2, HttpSession session, Model model)throws IOException{
        List<ProductAttributeCategory> list = pacService.queryAllByLimit(0, 10000);
        model.addAttribute("ppa",list);
        //文件上传类路径下的地址
        String path = ResourceUtils.getURL("classpath:").getPath()+"static/upload";
        String realPath = path.replace('/', '\\').substring(1,path.length());
        //查看路径是否正确
        System.out.println(realPath);

        if(!Objects.requireNonNull(file.getOriginalFilename()).isEmpty()){
            //获取文件的加密名称
            final String fileName = UUID.randomUUID()+file.getOriginalFilename();
            //限制文件上传的类型
            File logo = new File(realPath,fileName);
            String path1 = "../upload/"+fileName;
            file.transferTo(logo);
            product2.setPic(path1);
        }

        session.setAttribute("product2",product2);
        return "goods/productAttribute";
    }

    @PostMapping("/productAttribute/save")
    public ModelAndView save( HttpSession session, SkuStock skuStock, String color)  {
        ModelAndView modelAndView = new ModelAndView( "redirect:/goods/productList");
        Product product = (Product) session.getAttribute("product2");
        Product p = (Product) session.getAttribute("product");

        product.setProductCategoryId(p.getProductCategoryId());
        product.setName(p.getName());
        product.setSubTitle(p.getSubTitle());
        product.setBrandId(p.getBrandId());
        product.setDescription(p.getDescription());
        product.setProductSn(p.getProductSn());
        product.setPrice(p.getPrice());
        product.setOriginalPrice(p.getOriginalPrice());
        product.setStock(p.getStock());
        product.setUnit(p.getUnit());
        product.setWeight(p.getWeight());
        product.setSort(p.getSort());
        String spData="[{'key':'颜色','value':'"+color+"'}]";
        skuStock.setSpData(spData);
        skuStock.setSkuCode(UUID.randomUUID().toString().replaceAll("-",""));
        productService.insert(product);
        skuStockService.insert(skuStock);
        return modelAndView;
    }

}
